package com.ee.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.ee.bean.User;
import com.ee.bean.Workflow;
import com.ee.dao.WorkflowDao;

@Component("workflowDao")
public class WorkflowDaoImpl implements WorkflowDao {

	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	@Override
	public void saveWorkflow(Workflow workflow) {
		hibernateTemplate.save(workflow);
	}

	@Override
	public void deleteWorkflow(Workflow workflow) {
		hibernateTemplate.delete(workflow);
	}

	@Override
	public Workflow findWorkflowById(int id) {
		return hibernateTemplate.get(Workflow.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Workflow> findAllWorkflows() {

		String hql = "from Workflow workflow order by workflow.id";
		return (List<Workflow>)hibernateTemplate.find(hql);
	}

	@Override
	public void update(Workflow workflow) {
		hibernateTemplate.update(workflow);
	}

	@Override
	public List<Workflow> findAllWorkflowsByUser(User user) {
		String hql = "from Workflow workflow where workflow.user.id = " + user.getId() + " order by workflow.id" ;
		return (List<Workflow>)hibernateTemplate.find(hql);
	}

}
